package cn.cloudreal.cbms.system.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import cn.cloudreal.cbms.common.util.Constants;
import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.system.dao.SampleDao;
import cn.cloudreal.cbms.vo.Sample;

/**
 * <p>
 * Title: ITC
 * </p>
 * <p>
 * Description:查询数据Dao层实现类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2010
 * </p>
 * <p>
 * Company:云恒瑞通
 * </p>
 * 
 * @author chendanggui
 * @date 2011-11-02
 */
public class SampleDaoImpl extends SqlMapClientDaoSupport implements SampleDao
{
    private final String GET_ALL_SAMPLES = "sample.getAll";
    private final String GET_SAMPLES_BY_ID = "sample.getSample";
    private final String UPDATE_SAMPLE = "sample.update";
    private final String DEL_SAMPLE_BY_ID = "sample.deleteById";
    private final String ADD_SAMPLE = "sample.insert";
    private final String GET_ALL_SAMPLE_COUNT = "sample.getAllCount";

    /** 日志工具类实例 */
    private static final Logger log = Logger.getLogger(SampleDaoImpl.class);

    /**
     * 查询列表数据
     * 
     * @param page
     * @return List<Sample>
     */
    @SuppressWarnings("unchecked")
    public List<Sample> getSamples(PageBean page)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter SampleDaoImpl getSamples() method,return List<Sample>");
        }
        log.debug(page);

        int totalCount = (Integer) getSqlMapClientTemplate().queryForObject(GET_ALL_SAMPLE_COUNT);

        List<Sample> samples = null;

        // 判断 totalCount 是否 >0
        if (totalCount > Constants.CONST_0)
        {
            page.setTotalRecords(totalCount);

            Map<String, Integer> param = new HashMap<String, Integer>();
            param.put("pageNo", page.getPageNo());
            param.put("pagesize", page.getPageSize());

            samples = getSqlMapClientTemplate().queryForList(GET_ALL_SAMPLES,
                    param);
        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit SampleDaoImpl getSamples() method,return List<Sample>");
        }
        return samples;
    }

    /**
     * 根据id查询单个数据
     * 
     * @param page
     * @return Sample
     */
    public Sample getSample(long id)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter SampleDaoImpl getSample() method,return Sample");
        }

        // 执行当个sample 对象查询
        Sample sample = (Sample) getSqlMapClientTemplate().queryForObject(GET_SAMPLES_BY_ID,
                id);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit SampleDaoImpl getSample() method,return Sample");
        }
        return sample;
    }

    /**
     * 更新数据
     * 
     * @param page
     * @return
     */
    public void updateSample(Sample sample)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter SampleDaoImpl updateSample() method");
        }

        // 执行修改操作
        getSqlMapClientTemplate().update(UPDATE_SAMPLE, sample);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit SampleDaoImpl updateSample() method");
        }
    }

    /**
     * 删除数据
     * 
     * @param page
     * @return
     */
    public void delSample(long id)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter SampleDaoImpl delSample() method");
        }

        // 执行删除操作
        getSqlMapClientTemplate().delete(DEL_SAMPLE_BY_ID, id);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit SampleDaoImpl delSample() method");
        }
    }

    /**
     * 新增数据
     * 
     * @param page
     * @return
     */
    public void addSample(Sample sample)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter SampleDaoImpl addSample() method");
        }

        // 执行添加操作
        getSqlMapClientTemplate().insert(ADD_SAMPLE, sample);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit SampleDaoImpl addSample() method");
        }
    }

}
